﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ImageAnalysisLib
{
    public static class ImageAnalysisHelpers
    {
        public static Thickness GetImageBorderAnalysis(string filePath)
        {
            Thickness cropping = new Thickness(-1, -1, -1, -1);

            //byte[] pixels = new byte[4];
            //FormatConvertedBitmap source = new FormatConvertedBitmap();
            //source.BeginInit();
            //source.Source = ;
            //source.EndInit();
            int height = 0;
            int width = 0;
            byte[] pixelByteArray = null;
                //croppingControl1.Source = new BitmapImage(new Uri(file));
                ImageSource ims = new BitmapImage(new Uri(filePath));
                BitmapImage bitmapImage = (BitmapImage)ims;
                height = bitmapImage.PixelHeight;
                width = bitmapImage.PixelWidth;
                int nStride = (bitmapImage.PixelWidth * bitmapImage.Format.BitsPerPixel + 7) / 8;
                pixelByteArray = new byte[bitmapImage.PixelHeight * nStride];
                bitmapImage.CopyPixels(pixelByteArray, nStride, 0);
                PixelColor[,] pixelColorArray = new PixelColor[width + 1, height + 1];
                int i = 0;
                for (int x = 0; x < height; x++)
                {
                    for (int y = 0; y < width; y++)
                    {
                        pixelColorArray[y, x].R = pixelByteArray[i];
                        pixelColorArray[y, x].G = pixelByteArray[i + 1];
                        pixelColorArray[y, x].B = pixelByteArray[i + 2];
                        pixelColorArray[y, x].A = pixelByteArray[i + 3];
                        i += 4;
                    }
                }
                pixelByteArray = null;

                Thickness smallCrop = new Thickness(width++, height++, 0, 0);

                //calculate top
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        if (pixelColorArray[x, y].R > 20 || pixelColorArray[x, y].G > 20 || pixelColorArray[x, y].B > 20)
                        {
                            var c = pixelColorArray[x, y];
                            if (y < smallCrop.Top)
                            {
                                smallCrop.Top = y;
                            }
                            break;
                        }
                    }
                }
                //calculate bottom
                for (int x = 0; x < width; x++)
                {
                    for (int y = height - 1; y > 0; y--)
                    {
                        if (pixelColorArray[x, y].R > 20 || pixelColorArray[x, y].G > 20 || pixelColorArray[x, y].B > 20)
                        {
                            var c = pixelColorArray[x, y];
                            if (y > smallCrop.Bottom)
                            {
                                smallCrop.Bottom = y;
                            }
                            break;
                        }
                    }
                }
                //calculate right
                for (int y = 0; y < height; y++)
                {
                    for (int x = width - 1; x > 0; x--)
                    {
                        if (pixelColorArray[x, y].R > 20 || pixelColorArray[x, y].G > 20 || pixelColorArray[x, y].B > 20)
                        {
                            var c = pixelColorArray[x, y];
                            if (x > smallCrop.Right)
                            {
                                smallCrop.Right = x;
                            }
                            break;
                        }
                    }
                }
                //calculate left
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        if (pixelColorArray[x, y].R > 20 || pixelColorArray[x, y].G > 20 || pixelColorArray[x, y].B > 20)
                        {
                            var c = pixelColorArray[x, y];
                            if (x < smallCrop.Left)
                            {
                                smallCrop.Left = x;
                            }
                            break;
                        }
                    }
                }


                if (cropping.Top > smallCrop.Top || cropping.Top == -1) { cropping.Top = smallCrop.Top; }
                if (cropping.Bottom < smallCrop.Bottom || cropping.Bottom == -1) { cropping.Bottom = smallCrop.Bottom; }
                if (cropping.Right < smallCrop.Right || cropping.Right == -1) { cropping.Right = smallCrop.Right; }
                if (cropping.Left > smallCrop.Left || cropping.Left == -1) { cropping.Left = smallCrop.Left; }
            //cropping = new Thickness(cropping.Bottom - 4, cropping.Top + 4, cropping.Right - 4, cropping.Left + 4);
            cropping.Bottom = height - cropping.Bottom;
            cropping.Right = width - cropping.Right;
            cropping.Bottom = cropping.Bottom % 2 == 0 ? cropping.Bottom : cropping.Bottom - 1;
            cropping.Top = cropping.Top % 2 == 0 ? cropping.Top : cropping.Top + 1;
            cropping.Left = cropping.Left % 2 == 0 ? cropping.Left : cropping.Left - 1;
            cropping.Right = cropping.Right % 2 == 0 ? cropping.Right : cropping.Right + 1;
            return cropping;
        }
    }

    public struct PixelColor
    {
        public byte A;
        public byte R;
        public byte G;
        public byte B;

        public Color ToColor()
        {
            return Color.FromArgb(A, R, G, B);
        }
    }
}
